package br.com.funaction.eventtcontrollerv2.controller;

import java.io.IOException;
import java.io.Serializable;
import java.sql.SQLException;
import java.util.Date;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

import br.com.funaction.eventtcontrollerv2.entities.Franquia;
import br.com.funaction.eventtcontrollerv2.entities.PDV;
import br.com.funaction.eventtcontrollerv2.model.PDVDAO;

@RequestScoped
@ManagedBean
public class VisualizaPDVMB implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	PDV pdv = new PDV();
	PDVDAO pdvDAO = new PDVDAO();
	Franquia franquia = new Franquia();
	FacesContext contexto = FacesContext.getCurrentInstance();
	private Date dataAtual = new Date(); 

	@PostConstruct
	public void init(){
		
		try{
			
			autenticacao();
			
		} catch(Exception e){
			
			this.franquia.setId(1);
		}
		
		try{
			
			listaTodosPDV();
			
		} catch(Exception e){
			
		}
	}
	
	private void autenticacao() throws IOException{
		
		franquia = (Franquia) contexto.getExternalContext().getSessionMap().get("franquia");
		if(franquia == null){
			
			ExternalContext contextoExterno = contexto.getExternalContext();
			contextoExterno.redirect("/eventt-controller-v2/jsf/login/login.jsf");
			return;
		}
		pdv.setIdFranquia(franquia.getId());
	}
	
	
	private void listaTodosPDV() throws SQLException{
		
		pdv.setListaPDV(pdvDAO.listaTodosPDVVisualizar(pdv));
	}
	
	public void carregaDadosPDVSelecionado() throws SQLException{
		
		int id = pdv.getId();
		pdv = pdvDAO.listaTudoPDV(id);
	}
	
	public PDV getPdv() {
		return pdv;
	}

	public void setPdv(PDV pdv) {
		this.pdv = pdv;
	}

	public Date getDataAtual() {
		return dataAtual;
	}

	public void setDataAtual(Date dataAtual) {
		this.dataAtual = dataAtual;
	}

}